import axios from "axios";
import { defineStore } from "pinia";
import { totalData } from "../miscellaneous/NavData";
import { ref } from "vue";
export const useShopperStore = defineStore("shopper", () => {
  const shopper = ref("");
  const hasCoupon = ref(false);

  const getShopperData = () => {
    return totalData.filter((item) => item.shopper === shopper.value);
  };

  const getAllData = () => {
    return totalData;
  };

  const getCurrentShopperData = () => {
    // 筛选出当前商家的所有商品
    if (!shopper.value) {
      console.warn("当前商家名称为空");
      return [];
    }
    return totalData.filter((item) => item.shopper === shopper.value);
  };

  const setShopper = (name) => {
    shopper.value = name;
  };

  const reset = () => {
    shopper.value = "";
    hasCoupon.value = false;
  };
    const getOneComment = async() => {
     const res = await axios.get(
       "https://api.bugpk.com/api/random_comments?encode=json"
     );
     return res.data.text
  };

  return {
    shopper,
    hasCoupon,
    getShopperData,
    getAllData,
    getCurrentShopperData,
    setShopper,
      reset,
      getOneComment,
  };
});
